@layout('layouts.system')

@section('content')
<div class="page-header">
    <h3>{{ Str::upper(Lang::line('admin.departmentconfig')->get()) }}</h3>
</div>

<div class="row-fluid">
	<a href="#addDeptModal" role="button" class="btn pull-right" data-toggle="modal" style='margin-bottom:10px'><i class="icon-plus"></i>&nbsp;{{ Str::title(Lang::line('admin.adddepartment')->get()) }}</a>
</div>
<div class="row-fluid">
	<div class="span12">
		<div id="listDeparment" class="row-fluid">
			{{ $listDeparment  }}
		</div>
	</div>
</div>

<!-- Modal -->
<div id="addDeptModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  <h3 id="myModalLabel">{{ Str::title(Lang::line('admin.adddepartment')->get()) }}</h3>
</div>
<div class="modal-body">
{{ Form::open('admin/user/role', 'POST', array('id' => 'addDeptForm', 'class' => 'form-horizontal')) }}
{{ Form::hidden('wpj_no_auto',NULL)}}
{{ Form::control_group(Form::label('wpj_jabatan_name', Str::title(Lang::line('admin.department')->get())),Form::large_text('wpj_jabatan_name',null,array('placeholder'=>Str::title(Lang::line('global.type')->get().' '.Lang::line('admin.department')->get()),'required')),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>')) }}
{{ Form::control_group(Form::label('wpj_jabatan_desc', Str::title(Lang::line('global.desc')->get())),Form::large_text('wpj_jabatan_desc', '', array('placeholder'=>Str::title(Lang::line('global.type')->get().' '.Lang::line('global.desc')->get())))) }}
{{ Form::close()}}
</div>
<div class="modal-footer">
  <button class="btn" data-dismiss="modal" aria-hidden="true">{{ Str::title(Lang::line('global.close')->get()) }}</button>
  <button id="addDeptBtn" class="btn btn-primary">{{ Str::title(Lang::line('global.save')->get()) }}</button>
</div>
</div>

<div id="editDeptModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  <h3 id="myModalLabel">{{ Str::title(Lang::line('admin.editdepartment')->get()) }}</h3>
</div>
<div class="modal-body">
{{ Form::open('admin/user/role', 'POST', array('id' => 'editDeptForm', 'class' => 'form-horizontal')) }}
{{ Form::hidden('wpj_no_auto',NULL)}}
{{ Form::control_group(Form::label('wpj_jabatan_name', Str::title(Lang::line('admin.department')->get())),Form::large_text('wpj_jabatan_name',null,array('placeholder'=>Str::title(Lang::line('global.type')->get().' '.Lang::line('admin.department')->get()),'required')),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>')) }}
{{ Form::control_group(Form::label('wpj_jabatan_desc', Str::title(Lang::line('global.desc')->get())),Form::large_text('wpj_jabatan_desc', '', array('placeholder'=>Str::title(Lang::line('global.type')->get().' '.Lang::line('global.desc')->get())))) }}
{{ Form::close()}}
</div>
<div class="modal-footer">
  <button class="btn" data-dismiss="modal" aria-hidden="true">{{ Str::title(Lang::line('global.close')->get()) }}</button>
  <button id="editDeptBtn" class="btn btn-primary">{{ Str::title(Lang::line('global.edit')->get()) }}</button>
</div>
</div>
@endsection
@section('scripts')
<script type="text/javascript">

$('#addDeptBtn').click(function() {
  $.post('department', $("#addDeptForm").serialize(),function(data) {
          sourcedata = data;
        }).success(function() { 
          sourcedata = jQuery.parseJSON(sourcedata);
          if(sourcedata.messages){
    		validated("addDeptForm",sourcedata.messages);
          }else{
            $("#listDeparment" ).empty().html( sourcedata );
            $("#addDeptForm :input").val('');
            $('#addDeptModal').modal('hide');
            window.location.replace('department');
          } 
        });
});

$('#editDeptBtn').click(function() {
  $.post('editDepartment', $("#editDeptForm").serialize(),function(data) {
          sourcedata = data;
        }).success(function() { 
          sourcedata = jQuery.parseJSON(sourcedata);
          if(sourcedata.messages){
    		validated("editDeptForm",sourcedata.messages);
          }else{
            $("#listDeparment" ).empty().html( sourcedata );
            window.location.replace('department');
          } 
        });
});

function editDept(id){
	$('#editDeptModal').modal('toggle');
	//$('#editDeptBtn .modal-header #myModalLabel').empty().text("{{ Str::title(Lang::line('global.edit')->get()) }} {{ Str::title(Lang::line('admin.department')->get()) }} ");
	
	  $.get('deptInfo', { id: id},function(data,status){
		for (x in data)
		{   
		  $('#editDeptForm input[name="'+ x +'"]' ).val(data[x]);

		}
	  },"json");
}

function deleteDept(id,name){
    //var r = confirm("{{ Str::title(Lang::line('global.delete')->get()) }} "+"{{ Str::title(Lang::line('admin.department')->get()) }} " +name.toUpperCase() + " ?");
    var r = confirm("{{ Lang::line('admin.deletejabatan')->get() }} " +name.toUpperCase() + " ?");

    if (r==true){
        $.post("deleteDepartment", "id="+id+"&name="+name ,function(data) {
              sourcedata = data;
            }).success(function() {
	            sourcedata = jQuery.parseJSON(sourcedata);
	            if(sourcedata.fail){
                	alert(sourcedata.fail);
              	}else{
					$( "#listDeparment" ).empty().append( sourcedata );
				}

				window.location.replace('department');
          	});
    }
}

</script>
@endsection